package com.mbc.rmt.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mbc.common.dao.DAO;
import com.mbc.rmt.entities.Confirm;

public class ConfirmDAOImpls extends DAO<Confirm> implements ConfirmDAO{

	public ConfirmDAOImpls(Connection conn){
		this.conn = conn;
		this.table_name = "Confirm";
	}
	@Override
	public void create(Confirm entity) throws SQLException {
		query = "insert into "
				+table_name
				+" (ReceiptID,ReceiptNo,Sender,PaymentCurrencyID,Rate,PaymentAmount," 
				+" ExtraFees,Receiver,NRC,Address,AddCodeID,ToAgentID,ToBranchID," 
				+" ReceivedCurrencyID,ReceivedAmount,Status) values ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		pstmt = conn.prepareStatement(query);
		int i=1;
		pstmt.setLong(i++, entity.getReceiptID());
		pstmt.setString(i++, entity.getReceiptNo());
		pstmt.setString(i++, entity.getSender());
		pstmt.setLong(i++, entity.getPaymentCurrencyId());
		pstmt.setDouble(i++, entity.getRate());
		pstmt.setDouble(i++, entity.getPaymentAmt());
		pstmt.setDouble(i++, entity.getExtraFees());
		pstmt.setString(i++, entity.getReceiver());
		pstmt.setString(i++, entity.getNrc());
		pstmt.setString(i++, entity.getAddress());
		pstmt.setLong(i++, entity.getAddCodeID());
		pstmt.setString(i++, entity.getCode());
		pstmt.setLong(i++, entity.getToAgentId());
		pstmt.setString(i++, entity.getToAgentCode());
		pstmt.setLong(i++, entity.getToBranchId());
		pstmt.setString(i++, entity.getToBranchCode());
		pstmt.setLong(i++, entity.getReceivedCurrencyId());
		pstmt.setString(i++, entity.getReceivedCurrencyCode());
		pstmt.setDouble(i++, entity.getReceivedAmt());
		pstmt.setLong(i++, entity.getStatus());
		pstmt.executeUpdate();
		pstmt.close();
	}

	@Override
	public void update(Confirm entity) throws SQLException {
		query = "update "
				+table_name
				+" set "
				+"(ReceiptNo = ?,Sender = ?,PaymentCurrencyID = ?,Rate = ?,PaymentAmount = ?," 
				+" ExtraFees = ?,Receiver = ?,NRC = ?,Address = ?,AddCodeID = ?,ToAgentID = ?,"
				+" ToBranchID = ?,ReceivedCurrencyID = ?,ReceivedAmount = ?,Status = ?) "
				+" where ReceiptID = "+entity.getReceiptID();
		pstmt = conn.prepareStatement(query);
		int i = 1;
		pstmt.setString(i++, entity.getReceiptNo());
		pstmt.setString(i++, entity.getSender());
		pstmt.setLong(i++, entity.getPaymentCurrencyId());
		pstmt.setString(i++, entity.getPaymentCurrencyCode());
		pstmt.setDouble(i++, entity.getRate());
		pstmt.setDouble(i++, entity.getPaymentAmt());
		pstmt.setDouble(i++, entity.getExtraFees());
		pstmt.setString(i++, entity.getReceiver());
		pstmt.setString(i++, entity.getNrc());
		pstmt.setString(i++, entity.getAddress());
		pstmt.setLong(i++, entity.getAddCodeID());
		pstmt.setString(i++, entity.getCode());
		pstmt.setLong(i++, entity.getToAgentId());
		pstmt.setString(i++, entity.getToAgentCode());
		pstmt.setLong(i++, entity.getToBranchId());
		pstmt.setString(i++, entity.getToBranchCode());
		pstmt.setLong(i++, entity.getReceivedCurrencyId());
		pstmt.setString(i++, entity.getReceivedCurrencyCode());
		pstmt.setDouble(i++, entity.getReceivedAmt());
		pstmt.setLong(i++, entity.getStatus());
		pstmt.executeUpdate();
		pstmt.close();
		
	}

	@Override
	public void delete(long key) throws SQLException {
		query = getDeleteQuery() + " where ReceiptID = " + key;
		pstmt = conn.prepareStatement(query);
		pstmt.executeUpdate();
		pstmt.close();
		
	}

	@Override
	public Confirm find(long key) throws SQLException {
		Confirm l_Confirm = null;
		query = "select * from "+table_name+"a where ReceiptID = "+key;
		pstmt = conn.prepareStatement(query);
		rs = pstmt.executeQuery();
		if(rs.next()){
			l_Confirm = readRecord(rs);
		}
		return l_Confirm;
	}

	@Override
	protected Confirm readRecord(ResultSet rs) throws SQLException {
		Confirm l_Confirm = new Confirm();
		l_Confirm.setReceiptID(rs.getLong(""));
		l_Confirm.setReceiptNo(rs.getString(""));
		l_Confirm.setSender(rs.getString(""));
		l_Confirm.setPaymentCurrencyId(rs.getLong(""));
		l_Confirm.setPaymentCurrencyCode(rs.getString(""));
		
		return l_Confirm;
	}

}
